//
//  PickViewPhotoTableViewCell.m
//  CarTown
//
//  Created by 熊浩 on 15/8/30.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "PickViewPhotoTableViewCell.h"
#import "LCActionSheet.h"
#import "SDPhotoBrowser.h"
#import "CarpubMode.h"
#import <UIButton+WebCache.h>

@interface PickViewPhotoTableViewCell ()<LCActionSheetDelegate,SDPhotoBrowserDelegate>

@end

@implementation PickViewPhotoTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UILabel *titlelable=[[UILabel alloc]initWithFrame:CGRectMake(8, 8, XHScreenwidth-16, 18)];
        titlelable.font=[UIFont systemFontOfSize:15];
        titlelable.textColor=textnormalGary;
        [self.contentView addSubview:titlelable];
        _titlelable=titlelable;
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 34, XHScreenwidth, 1)];
        view.backgroundColor=BoderColor;
        [self.contentView addSubview: view];
        
        CGFloat width=60;
        UIView *photoview=[[UIView alloc]initWithFrame:CGRectMake(0, 35, XHScreenwidth, width+16)];
        [self.contentView addSubview:photoview];
        _photoview=photoview;
        CGFloat x;
        CGFloat y;
        CGFloat height=width;
        CGFloat boder=8;
        for (int i=0; i<8; i++) {
            UIButton *btn=[[UIButton alloc]init];
            [btn setImage:[UIImage imageNamed:@"button_photo"] forState:UIControlStateNormal];
            int col=i%4;
            int row=i/4;
            x=XHScreenwidth-280+(boder+width)*col;
            y=8+(8+height)*row;
            btn.frame=CGRectMake(x, y, width, height);
            btn.tag=i;
            btn.hidden=!(i==7);
            btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
            
            [btn addTarget:self action:@selector(pickerbtnclick:) forControlEvents:UIControlEventTouchUpInside];
            [photoview addSubview:btn];
          
        }
        photoview.height=2*(height+8)+8;
    }
    return self;
}
-(void)setMode:(CarpubMode *)mode{
    _mode=mode;
    self.titlelable.text=mode.name;
    if (_mode.urlarray.count>0&&_mode.isHasURL) {
        [self setupimagearray];
    }else{
        
         [self reloadImage];
    }
}
-(void)setupimagearray{
    
    
    for (int i=0; i<_mode.urlarray.count; i++) {
        NSString *url=_mode.urlarray[i];
        UIButton *btn=self.photoview.subviews[i];
        [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:url]] forState:UIControlStateSelected placeholderImage:[UIImage imageNamed:@"placehoder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [_mode.imgarray addObject:image];
            if (i==_mode.imgarray.count-1) {
                
                [self reloadImage];
               
            }
            
        }];
    }
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)reloadImage{
    _mode.isHasURL=NO;
    for (int i=0; i<8; i++) {
        UIButton *btn=self.photoview.subviews[i];
        if (i<_mode.imgarray.count) {
            [btn setImage:_mode.imgarray[i] forState:UIControlStateSelected];
            btn.selected=YES;
            btn.hidden=NO;
        }else{
            btn.selected=NO;
            if (_mode.imgarray.count<4&&i==3) {
                btn.hidden=NO;
            }else if(_mode.imgarray.count>=4&&i==7){
                btn.hidden=NO;
            }else{
                btn.hidden=YES;
            }
        }
    }

}

-(void)pickerbtnclick:(UIButton *)btn{
   
    if (btn.isSelected) {
        [self showhasactionsheet:btn.tag];
    }else{
        [self showactionsheet:btn.tag];
    }
}
+(instancetype)cellWithTablView:(UITableView *)tablview{
    
    static NSString *picker=@"PICKERPHOTO";
    PickViewPhotoTableViewCell *cell=[tablview dequeueReusableCellWithIdentifier:picker];
    if (cell==nil) {
        cell=[[PickViewPhotoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:picker];
    }
    return cell;
}


-(void)showactionsheet:(NSInteger)tag{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles: @[@"拍照",@"从相册中选取"] redButtonIndex:-1 delegate:self];
    sheet.isTag=NO;
    sheet.tag=tag;
    [sheet show];
}
-(void)showhasactionsheet:(NSInteger)tag{
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles: @[@"重新上传",@"查看大图",@"删除"] redButtonIndex:2 delegate:self];
    sheet.isTag=YES;
    sheet.tag=tag;
    [sheet show];
}

-(void)removeImageFromArray:(NSInteger)index{
    [self.mode.imgarray removeObjectAtIndex:index];
    self.mode.ischange=YES;
    [self reloadImage];
    if ([_delegate respondsToSelector:@selector(photoCellViewReloadImage:)]) {
        [_delegate photoCellViewReloadImage:self];
    }
  
}

-(void)showImageWith:(NSInteger)index{
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]init];
    browser.sourceImagesContainerView = self.photoview; // 原图的父控件
    browser.imageCount = _mode.imgarray.count; // 图片总数
    browser.currentImageIndex = index;
    browser.delegate = self;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    return self.mode.imgarray[index];
}

-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==actionSheet.btnCount-1) {
        return;
    }
    if (actionSheet.isTag) {
        switch (buttonIndex) {
            case 0:
                [self showactionsheet:actionSheet.tag];
                break;
            case 1:
                 [self showImageWith:actionSheet.tag];
                break;
            case 2:
            {
                [self removeImageFromArray:actionSheet.tag];
             }
                break;
            default:
                break;
        }
        
       
        
    }else{
        [self showImagepicker:actionSheet.tag ActionSheetIndex:buttonIndex];
    }
}

-(void)showImagepicker:(NSInteger)index ActionSheetIndex:(NSInteger) actionindex{
    if ([_delegate respondsToSelector:@selector(photoCellView:pickerBtnClick:withButton:)]) {
        [_delegate photoCellView:self pickerBtnClick:actionindex withButton: _photoview.subviews[index]];
    }
   
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
